﻿using System;
using System.Activities.Hosting;
using System.Activities.Persistence;
using System.Activities.Tracking;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuditSample.Persistence
{
    public class StateParticipant_old : TrackingParticipant, IWorkflowInstanceExtension
    {
        PersistenceIOParticipant persistenceParticipant;

        public StateParticipant_old()
        {
            persistenceParticipant = new StateSavePersistenceParticipant(this);
        }

        public void Save()
        {
            //dbContext.SaveChanges();
        }

        protected override void Track(TrackingRecord record, TimeSpan timeout)
        {
            //get the custom record
            if (record != null)
            {
                if (record is CustomTrackingRecord)
                {
                    CustomTrackingRecord customRecord = (CustomTrackingRecord)record;
                    if (customRecord.Name.Equals("NextAudit", StringComparison.InvariantCultureIgnoreCase))
                    {
                        using (Model1 dbContext = new Model1())
                        {
                            WorkFlowInstance workflowInstance = dbContext.WorkFlowInstances.FirstOrDefault(x => x.Id == record.InstanceId.ToString());
                            workflowInstance.AuditRole = customRecord.Data.GetString("AuditRole");
                            workflowInstance.Auditer = customRecord.Data.GetString("Auditer");
                            workflowInstance.State = customRecord.Data.GetString("State");
                            workflowInstance.UpdatedDate = customRecord.Data.GetDate("Date");
                            workflowInstance.RecordNumber = customRecord.RecordNumber.ToString();
                            dbContext.SaveChanges();
                        }
                    }
                }
            }

        }



        IEnumerable<object> IWorkflowInstanceExtension.GetAdditionalExtensions()
        {
            return new object[] { this.persistenceParticipant };
        }

        public void SetInstance(WorkflowInstanceProxy instance)
        {
        }

        class StateSavePersistenceParticipant : PersistenceIOParticipant
        {
            StateParticipant_old Participant { get; set; }

            public StateSavePersistenceParticipant(StateParticipant_old participant)
                : base(true, false)
            {
                this.Participant = participant;
            }


            protected override IAsyncResult BeginOnSave(IDictionary<System.Xml.Linq.XName, object> readWriteValues, IDictionary<System.Xml.Linq.XName, object> writeOnlyValues, TimeSpan timeout, AsyncCallback callback, object state)
            {
                this.Participant.Save();
                return base.BeginOnSave(readWriteValues, writeOnlyValues, timeout, callback, state);
            }

            protected override void EndOnSave(IAsyncResult result)
            {
            }

            protected override void Abort()
            {
            }
        }
    }
}

